1 /*
2 Copyright (c)
2014 Andrew Jones, Dario Seyb
3  Based
on 'Spriter2Unity' python code by Malhavok
4
5 Permission
is hereby granted, free of charge, to any person obtaining a copy
6 of
this software and associated documentation files (the "Software"), to deal
7 in
the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software
is
10 furnished to
do so, subject to the following conditions:
11
12 The above copyright notice and
this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */

23 using
System;
24 using
System.Collections.Generic;
25 using
System.Linq;
26 using
System.Text;
27 using
System.Xml;
28
29 namespace
Assets.ThirdParty.Spriter2Unity.Editor.Spriter
30 {
31     
public enum LoopType
32     {
33         INVALID,
34         True,
35         False,
36         PingPong,
37     }
38
39     
public static class LoopTypeUtils
40     {
41         
public static LoopType Parse(XmlElement element)
42         {
43             
var looping = element.GetString("looping", "true");
44             
switch (looping)
45             {
46                 
case "true":
47                     
return LoopType.True;
48                 
case "false":
49                     
return LoopType.False;
50                 
case "ping_pong":
51                     
return LoopType.PingPong;
52             }
53             
return LoopType.INVALID;
54         }
55     }
56
57     
public class SpriterAnimation : KeyElem
58     {
59         
public const string XmlKey = "animation";
60
61         
public Entity Entity {get;private set;}
62         
public string Name { get; private set; }
63         
public int Length_Ms { get; private set; }
64         
public float Length { get { return ((float)Length_Ms) / 1000; } }
65         
public LoopType LoopType { get; private set; }
66         
public int LoopTo { get; private set; }
67         
public IEnumerable<MainlineKey> MainlineKeys { get { return mainlineKeys; } }
68         
public IEnumerable<Timeline> Timelines { get { return timelines; } }
69
70         
public SpriterAnimation(XmlElement element, Entity entity)
71             :
base(element)
72         {
73             Parse (element, entity);
74         }
75
76         
public Timeline GetTimeline(int id)
77         {
78             
return Timelines.Where(timeline => timeline.Id == id).FirstOrDefault();
79         }
80
81         
protected virtual void Parse(XmlElement element, Entity entity)
82         {
83             Entity = entity;
84
85             Name = element.GetString(
"name", "");
86             Length_Ms = element.GetInt(
"length", -1);
87             LoopType = LoopTypeUtils.Parse(element);
88             LoopTo = element.GetInt(
"loop_to", 0);
89
90             LoadTimelines(element);
91             LoadMainline(element);
92         }
93
94         
private void LoadMainline(XmlElement element)
95         {
96             
var mainlineElem = element["mainline"];
97             
var mainlineKeyElems = mainlineElem.GetElementsByTagName(Key.XmlKey);
98             
foreach (XmlElement mainlineKeyElem in mainlineKeyElems)
99             {
100                 mainlineKeys.Add(
new MainlineKey(mainlineKeyElem, this));
101             }
102         }
103
104         
private void LoadTimelines(XmlElement element)
105         {
106             
var timelineElems = element.GetElementsByTagName(Timeline.XmlKey);
107             
foreach(XmlElement timelineElem in timelineElems)
108             {
109                 timelines.Add(
new Timeline(timelineElem, this));
110             }
111         }
112
113         
private List<MainlineKey> mainlineKeys = new List<MainlineKey>();
114         
private List<Timeline> timelines = new List<Timeline>();
115     }
116 }



Trò chơi đua xe động vật trong UNITY Engine 114.902 lượt xem

Gõ tìm kiếm nhanh...